package com.tianma315.web.codes.controller;


import java.util.Arrays;

import com.tianma315.core.codes.domain.CodesDO;
import com.tianma315.core.codes.service.CodesService;
import com.tianma315.core.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2019-09-02 16:18:34 | Wen</small>
 */
@Controller
@RequestMapping("/codes/codes")
public class CodesController {
	@Autowired
	private CodesService codesService;
	
	@GetMapping()
	@RequiresPermissions("codes:codes:codes")
	String Codes(){
	    return "codes/codes/codes";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("codes:codes:codes")
	public Result<Page<CodesDO>> list(Integer pageNumber, Integer pageSize, CodesDO codesDTO){
		// 查询列表数据
        Page<CodesDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<CodesDO> wrapper = new EntityWrapper<CodesDO>(codesDTO);
        page = codesService.selectPage(page, wrapper);
        int total = codesService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("codes:codes:add")
	String add(){
	    return "codes/codes/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("codes:codes:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		CodesDO codes = codesService.selectById(id);
		model.addAttribute("codes", codes);
	    return "codes/codes/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("codes:codes:add")
	public Result<String> save( CodesDO codes){
		if(codesService.insert(codes)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("codes:codes:edit")
	public Result<String>  update( CodesDO codes){
		codesService.updateById(codes);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("codes:codes:remove")
	public Result<String>  remove( Integer id){
		if(codesService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("codes:codes:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		codesService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
